package org.supreme.org.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.supreme.org.domain.Department;
import org.supreme.org.mapper.DepartmentMapper;
import org.supreme.org.service.IDepartmentService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author director
 * @since 2022-12-04
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {
        @Autowired
        private DepartmentMapper departmentMapper;

    @Override
    public List<Department> tree() {
        //查询所有
        List<Department> list = departmentMapper.selectList(null);
        //通过Steam流拿到
        Map<Long, Department> collect = list.stream().collect(Collectors.toMap(Department::getId, e -> e));
        List<Department> departments = new ArrayList<>();
        for (Department dept : list){
            if (dept.getParentId()==null){
                departments.add(dept);
            } else {
                Department department = collect.get(dept.getParentId());
                List<Department> children = department.getChildren();
                children.add(dept);
            }
        }

        return departments;
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        departmentMapper.deleteBatchIds(ids);
    }
//        List<Department> list = departmentMapper.selectList(null);
//        Map<Long, Department> collect = list.stream().collect(Collectors.toMap(Department::getId, e -> e));
//        ArrayList<Department> departments = new ArrayList<>();
//        for (Department department : list){
//            if (department.getParentId()==null){
//                departments.add(department);
//            }else {
//                Department department1 = collect.get(department.getParentId());
//                List<Department> children = department1.getChildren();
//                children.add(department);
//            }
//        }
//        return departments;
   // }

}